package android.activities;

import android.app.Activity;
import android.content.Intent;
import android.database.Database;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class LoginActivity extends Activity implements OnClickListener{
	
	int position = 0;
	EditText text;
	public static final String KLUCZ = "android.activities.ID";
	
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.login);	     
	     position = Integer.parseInt(getIntent().getStringExtra(KLUCZ));
	     View button16 = findViewById(R.id.button16);
	     button16.setOnClickListener(this);
	     View button22 = findViewById(R.id.button22);
	     button22.setOnClickListener(this);
	     text = (EditText)findViewById(R.id.editText12);
	 }

	public void onClick(View v) {
		switch(v.getId()) {
    	case R.id.button22:
    		finish();   		
    		break;
    	case R.id.button16:
			String password = text.getText().toString();
			Database database = new Database(this);		
			String baza = database.getBaseName(position);
			boolean condition = database.checkPassword(baza, password);
			if(condition == true) {
				Intent i = new Intent(LoginActivity.this, BaseActivity.class);
				i.putExtra(BaseActivity.KLUCZ, String.valueOf(position));
				i.putExtra(BaseActivity.KLUCZ2, password);
				startActivity(i);
				finish();
				break;
			}
			else {
				long time = 1000;
		        try {
					Thread.sleep(time);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Intent i = new Intent(this, WrongPasswordActivity.class);
    			startActivity(i);
			}
		}
	}

}
